﻿
using System;
using RpcHelper;
using RpcModel;
using SqlExecHelper;
using SqlExecHelper.SetColumn;
using Wedonek.RpcStore.Service.Helper;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Service.DAL
{
        internal class AutoTaskDAL : SqlBasicClass
        {
                public AutoTaskDAL () : base ("AutoTaskList", "RpcExtendService")
                {

                }

                public Guid Add (AutoTask task)
                {
                        task.Id = Tools.NewGuid ();
                        this.Insert (task);
                        return task.Id;
                }
                public void Drop (Guid id)
                {
                        if (!base.Drop ("Id", id))
                        {
                                throw new ErrorException ("rpc.auto.task.drop.error");
                        }
                }
                public AutoTaskList Get (Guid id)
                {
                        return base.GetRow<Guid, AutoTaskList> ("Id", id);
                }
                public void SetAutoTask (Guid id, AutoTaskSetParam param)
                {
                        if (!base.Update (new ISqlSetColumn[] {
                                new SqlSetColumn("TaskName", System.Data.SqlDbType.NVarChar,50){ Value=param.TaskName},
                                new SqlSetColumn("TaskShow", System.Data.SqlDbType.NVarChar,255){ Value=param.TaskShow},
                                new SqlSetColumn("TaskPriority", System.Data.SqlDbType.Int){ Value=param.TaskPriority},
                                new SqlSetColumn("BeginItem", System.Data.SqlDbType.SmallInt){ Value=param.BeginItem},
                                new SqlSetColumn("FailEmall", System.Data.SqlDbType.VarChar,1000){ Value=param.FailEmall},
                                new SqlSetColumn("VerNum", System.Data.SqlDbType.Int){ Value=Tools.GetRandom()}
                        }, "Id", id))
                        {
                                throw new ErrorException ("rpc.task.set.fail");
                        }
                }

                internal void SetIsEnable (Guid id, bool isEnable)
                {
                        if (!base.Update (new ISqlSetColumn[] {
                                new SqlSetColumn("IsEnable", System.Data.SqlDbType.Bit){ Value=isEnable}
                        }, "Id", id))
                        {
                                throw new ErrorException ("rpc.task.set.fail");
                        }
                }
                public void ResetVerNum (Guid id)
                {
                        if (!base.Update (new ISqlSetColumn[] {
                                new SqlSetColumn("VerNum", System.Data.SqlDbType.Int){ Value=Tools.GetRandom()}
                        }, "Id", id))
                        {
                                throw new ErrorException ("rpc.task.set.fail");
                        }
                }

                public AutoTaskDatum[] QueryTask (QueryTaskParam query, IBasicPage paging, out long count)
                {
                        paging.InitOrderBy ("Id", true);
                        return base.Query<AutoTaskDatum> (paging.OrderBy, paging.Index, paging.Size, out count, query.ToWhere ());
                }
        }
}
